/**
 * %%
 * %%
 */
package com.leehom.arch.base.reportquery;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.swing.SortOrder;

import com.leehom.arch.base.utils.DateTimeUtils;
import com.leehom.arch.base.utils.StringUtils;

/**
 * @类名: TypeEnum
 * @说明: 参数类型枚举
 *
 * @author   leehom
 * @Date	 2020年9月3日 下午6:01:12s
 * 修改记录：
 *
 * @see 	 
 */
public enum TypeEnum {

	BOOL("boolean", "布尔型", Boolean.class, 
			(String valueStr)->Boolean.valueOf(valueStr)
		),
	SHORT("short", "短整型", Short.class, 
			(String valueStr)->Short.valueOf(valueStr)
		),
	INT("int", "整型", Integer.class,
			(String valueStr)->Integer.valueOf(valueStr)
		),
	LONG("long", "长整型", Long.class, 
			(String valueStr)->
				StringUtils.isEmpty(valueStr) ? null : Long.valueOf(valueStr)
		),
	FLOAT("float", "浮点型", Float.class,
			(String valueStr)->Float.valueOf(valueStr)
		),
	DOUBLE("double", "双精度浮点型", Double.class,
			(String valueStr)->Double.valueOf(valueStr)
		),
	STRING("string", "字符串型", String.class,
			(String valueStr)->valueStr
		),
	STRINGS("strings", "字符串型数组", String[].class,
			(String valueStr)->StringUtils.str2List(valueStr, ",")
		),
	DATE("date", "日期型", Date.class,
			(String valueStr)->DateTimeUtils.StringToDate(valueStr)
	),	
	ENUMCONST("enumconst", "枚举项", Enum.class,
			(String valueStr)->valueStr
	),
	ENUM("enum", "枚举", Enum.class,
			(String valueStr)->valueStr
	),
	ORDER("order", "排序", SortOrder.class,
			(String valueStr)->SortOrder.values()[Integer.valueOf(valueStr)]
	),
	LISTABLE("lisable", "可列表", Object.class,
			(String valueStr)->valueStr
	);
	
	private static Map<String, Class<?>> nameClazzMap; 
	private static Map<String, TypeEnum> typeMap; 
	
	static {
		nameClazzMap = new HashMap<>();
		nameClazzMap.put("BOOLEAN", Boolean.class);
		nameClazzMap.put("SHORT", Short.class);
		nameClazzMap.put("INT", Integer.class);
		nameClazzMap.put("LONG", Long.class);
		nameClazzMap.put("FLOAT", Float.class);
		nameClazzMap.put("DOUBLE", Double.class);
		nameClazzMap.put("STRING", String.class);
		nameClazzMap.put("DATE", Date.class);
		//
		typeMap = new HashMap<>(); 
		typeMap.put("BOOLEAN", BOOL);
		typeMap.put("SHORT", SHORT);
		typeMap.put("INT", INT);
		typeMap.put("LONG", LONG);
		typeMap.put("FLOAT", FLOAT);
		typeMap.put("DOUBLE", DOUBLE);

	}
	
	// 获取类型
	public static Class<?> nameToClazz(String name) {
		return nameClazzMap.get(name);
	}
	
	//
	public static TypeEnum nameToType(String name) {
		return typeMap.get(name);
	}
	
	private StringCast caster;

	TypeEnum(String name, String alias, Class<?> clazz, StringCast caster) {
		this.caster = caster;
	}
	
	public Object cast(String valueStr) {
		return caster.cast(valueStr);
	}

}
